package com.eschool.controller;

import com.eschool.pojo.*;
import com.eschool.service.StudentService;
import com.eschool.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Configuration
public class StudentController {
    @Autowired
    StudentService studentService;

    @RequestMapping("/student/Login")
    public ServerResult Login(String studentId,String password){
        StudentDAO studentDAO = studentService.Login(studentId,password);
        if(studentDAO!=null){
            return new ServerResult(0,"success",studentDAO);
        }
        else{
            return new ServerResult(500,"fail",null);
        }
    }

    @RequestMapping("/student/findByID")
    public ServerResult findByID(String studentId){
        StudentDAO  studentDAO = studentService.findById(studentId);
        if(studentDAO!=null){
            return new ServerResult(0,"success",studentDAO);
        }
        else {
            return new ServerResult(200,"fail",null);
        }
    }

    @RequestMapping("/student/changeInfo")
    public ServerResult changeInfo(String studentId,StudentChangeDTO  studentChangeDTO){
        int updateRow = studentService.StudentChangeInfo(studentId,studentChangeDTO);
        if(updateRow>0){
            return new ServerResult(0,"success",null);
        }
        else {
            return new ServerResult(200,"fail",null);
        }
    }

    @RequestMapping("/student/changePassword")
    public ServerResult changePassword(String studentId,String oldPassword,String newPassword){
        int updateRow = studentService.StudentChangePassword(studentId,oldPassword,newPassword);
        if(updateRow>0){
            return new ServerResult(0,"success",null);
        }
        else if(updateRow==-1){
            return new ServerResult(800,"old password is wrong",null);
        }
        else{
            return new ServerResult(200,"fail",null);
        }
    }
}
